<?php


namespace app\middleware;

use app\BaseController;

use app\controller\BlackConfigurationWarehouseController;
use app\lib\exception\AuthException;
use app\lib\exception\ConfigurationException;
use app\lib\exception\ParamException;
use app\services\AuthService;
use think\App;
use think\Container;
use think\facade\Cache;
use think\Request;
use think\Response;

class BlackConfigurationWarehouseStartMiddleware extends BaseController
{

    /**
     * @var \think\cache\Driver
     */
    private $redis;
    private $leisureKey = 'cod_black_leisure_configuration_warehouse';
    private $busyKey = 'cod_black_busy_configuration_warehouse';
    private $bcwArr = [
        'cod1'=>[
            "ZUH140"=>['user'=>'140api15', 'pwd'=> '83kdksls902'],
            "ZUH118"=>['user'=>'118api7', 'pwd'=> 'epe3pjl38x8s'],
            "MFM327"=>['user'=>'GUIHONG327', 'pwd'=> 'p5es4d85'],
        ],
        'cod2'=>[
            "ZUH140"=>['user'=>'CODAPI140', 'pwd'=> 'epe3pjl38x8s'],
            "ZUH118"=>['user'=>'CODAPI118', 'pwd'=> 'epe3pjl38x8s'],
            "MFM327"=>['user'=>'CODAPI327', 'pwd'=> 'epe3pjl38x8s'],
        ]
    ] ;

    public function __construct(App $app)
    {

        $this->redis =  Cache::store('redis');
        parent::__construct($app);
    }


    public function handle(Request $request, \Closure $next)
    {
        $leisure =  $this->redis->lRange($this->leisureKey, 0, -1);

        if (count($leisure)<1) throw new ConfigurationException('配置全部分配，过一下重试');
        $this->redis->lRem($this->leisureKey,$leisure[0],1);
        $this->redis->lPush($this->busyKey,$leisure[0]);


        $request->bcw = ['id'=>$leisure[0], 'data'=>$this->bcwArr[$leisure[0]]];
        return $next($request);
    }




}